/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FzEvents;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.FzEventsRepository;
import me.zhengjie.service.FzEventsService;
import me.zhengjie.service.dto.FzEventsDto;
import me.zhengjie.service.dto.FzEventsQueryCriteria;
import me.zhengjie.service.mapstruct.FzEventsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zcc
* @date 2021-07-18
**/
@Service
@RequiredArgsConstructor
public class FzEventsServiceImpl implements FzEventsService {

    private final FzEventsRepository fzEventsRepository;
    private final FzEventsMapper fzEventsMapper;

    @Override
    public Map<String,Object> queryAll(FzEventsQueryCriteria criteria, Pageable pageable){
        Page<FzEvents> page = fzEventsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fzEventsMapper::toDto));
    }

    @Override
    public List<FzEventsDto> queryAll(FzEventsQueryCriteria criteria){
        return fzEventsMapper.toDto(fzEventsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FzEventsDto findById(String id) {
        FzEvents fzEvents = fzEventsRepository.findById(id).orElseGet(FzEvents::new);
        ValidationUtil.isNull(fzEvents.getId(),"FzEvents","id",id);
        return fzEventsMapper.toDto(fzEvents);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FzEventsDto create(FzEvents resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return fzEventsMapper.toDto(fzEventsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FzEvents resources) {
        FzEvents fzEvents = fzEventsRepository.findById(resources.getId()).orElseGet(FzEvents::new);
        ValidationUtil.isNull( fzEvents.getId(),"FzEvents","id",resources.getId());
        fzEvents.copy(resources);
        fzEventsRepository.save(fzEvents);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            fzEventsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FzEventsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FzEventsDto fzEvents : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("排序", fzEvents.getSort());
            map.put("日期", fzEvents.getDateTime());
            map.put("事项", fzEvents.getSubjectName());
            map.put("事件", fzEvents.getContent());
            map.put("相关人员", fzEvents.getPerson());
            map.put("价格", fzEvents.getMoney());
            map.put("备注", fzEvents.getRemark());
            map.put("收支类型", fzEvents.getSubType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}